<?php


namespace app\admin\controller;

use think\Hook;
use think\Request;
use think\Db;

class Matter extends ApiCommon
{
    /**
     * 用于判断权限
     * @permission 无限制
     * @allow 登录用户可访问
     * @other 其他根据系统设置
    **/    
    public function _initialize()
    {
        $action = [
            'permission'=>[''],
            'allow'=>['index','save','update','delete']
        ];
        Hook::listen('check_auth',$action);
        $request = Request::instance();
        $a = strtolower($request->action());        
        if (!in_array($a, $action['permission'])) {
            parent::_initialize();
        }
    }

    /**
     * 获取办件事项列表
     * @return
     */
    public function index()
    {
        $param = $this->param;
        $matterModel = model('Matter');
        $data = $matterModel->getDataList($param);
        return resultArray(['data' => $data]);
    }

    /**
     * 新增办件事项
     */
    public function save()
    {
        $param = $this->param;
        $insertData=[
            "matter"=>$param["matter"],
            "money"=>$param["money"],
            "created_time"=>date("Y-m-d H:i:s"),
        ];
        $res=db("admin_matter")->insertGetId($insertData);
        if (!$res) {
            return resultArray(['error' => "办件事项新增失败！"]);
        }
        return resultArray(['data' => '办件事项新增成功！']);
    }

    /**
     * 编辑办件事项
     * @param 
     * @return
     */
    public function update()
    {
        $param = $this->param;
        $data=db("admin_matter")->where(["id"=>$param["id"]])->find();
        if(!$data) return resultArray(['error' => "该条信息不存在或已被删除！请刷新页面！"]);
        $insertData=[
            "matter"=>$param["matter"],
            "money"=>$param["money"],
            "created_time"=>date("Y-m-d H:i:s"),
        ];
        $res=db("admin_matter")->where(["id"=>$data["id"]])->update($insertData);
        if (!$res) {
            return resultArray(['error' => "办件事项编辑失败！"]);
        }
        return resultArray(['data' => '办件事项编辑成功！']);
    }

    /**
     * 删除办件事项
     * @param
     * @return
     */
    public function delete()
    {
        $param = $this->param;
        $data=db("admin_matter")->where(["id"=>$param["id"]])->find();
        if(!$data) return resultArray(['error' => "该条信息不存在或已被删除！请刷新页面！"]);
        $res=db("admin_matter")->where(["id"=>$data['id']])->delete();
        if (!$res) {
            return resultArray(['error' => "删除失败！"]);
        }
        return resultArray(['data' => '删除成功']);
    }

}
